package per.wsj.room.data

import android.content.Context
import androidx.annotation.NonNull
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import per.wsj.room.data.dao.UserDao
import per.wsj.room.data.entity.User

@Database(entities = [User::class], version = 2, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {

    abstract fun userDao(): UserDao

    companion object {
        val DATABASE_NAME = "user-db.db"

        // For Singleton instantiation
        @Volatile
        private var instance: AppDatabase? = null

        fun getInstance(context: Context): AppDatabase {
            return instance ?: synchronized(this) {
                instance ?: buildDatabase(context).also { instance = it }
            }
        }

        // Create and pre-populate the database. See this article for more details:
        // https://medium.com/google-developers/7-pro-tips-for-room-fbadea4bfbd1#4785
        private fun buildDatabase(context: Context): AppDatabase {
            return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
                .addCallback(dbCallback)
                .addMigrations(MIGRATION1_2)
                // To prevent the app from crashing when this situation occurs, call
                // the fallbackToDestructiveMigration() builder method when creating the database:
                .fallbackToDestructiveMigration()
                .build()
        }
    }

    object MIGRATION1_2 : Migration(1, 2) {
        override fun migrate(database: SupportSQLiteDatabase) {
            database.execSQL("ALTER TABLE user ADD COLUMN age INTEGER NOT NULL DEFAULT 18")
        }
    }

    // 数据库回调
    object dbCallback :RoomDatabase.Callback(){
        override fun onCreate(db: SupportSQLiteDatabase) {
            super.onCreate(db)
            // do something when database created
//            val request = OneTimeWorkRequestBuilder<SeedDatabaseWorker>().build()
//            WorkManager.getInstance(context).enqueue(request)
        }

        override fun onOpen(db: SupportSQLiteDatabase) {
            super.onOpen(db)
            // do something when database open
        }
    }

//    val MIGRATION_2_3: Migration = object : Migration(2, 3) {
//        override fun migrate(@NonNull database: SupportSQLiteDatabase) {
//            //1.创建一个新的符合Entity字段的新表user_new
//            database.execSQL(
//                "CREATE TABLE user_new (id INTEGER NOT NULL,"
//                        + "name TEXT NOT NULL,"       //关注点1
//                        + "age INTEGER NOT NULL,"     //关注点2
//                        + "weight TEXT,"              //关注点3
//                        + "sex TEXT,"
//                        + "PRIMARY KEY(id))"
//            )
//            //2.将旧表user中的数据拷贝到新表user_new中
//            database.execSQL(("INSERT INTO user_new(id,name,age,weight,sex) " + "SELECT id,name,age,weight,sex FROM user"))
//            //3.删除旧表user
//            database.execSQL("DROP TABLE user")
//            //4.将新表user_new重命名为user,升级完毕
//            database.execSQL("ALTER TABLE user_new RENAME TO user")
//        }
//    }
}